const router = require('koa-router')();
const advertService = require('../service/advertService');
router.prefix('/api/advert');

/**
 * @api {post} /api/advert/addAdvert 添加广告
 * @apiName addAdvert
 * @apiGroup advert
 *
 * @apiParam {String} title 广告标题.
 * @apiParam {String} type 类型（1：案例，2：服务）.
 *
 * @apiParam {String} target 目标为对应案例和服务的id.（类型为1时调用获取案例的接口，获取案例名和id；类型为2时调用获取服务的接口，获取服务名和id）
 *
 * @apiSuccess {int} code 返回状态码（1：添加成功，2：添加失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/addAdvert', async (ctx) => {
    try {
        let data = ctx.request.body;
        let title = data.title,
            type = data.type,
            target = data.target;
        if (!title || !type || !target) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await advertService.handleAddAdvert(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/advert/editAdvert 修改广告
 * @apiName editAdvert
 * @apiGroup advert
 *
 * @apiParam {String} id 类目id.
 * @apiParam {String} title 广告标题.
 * @apiParam {String} type 类型（1：案例，2：服务）.
 * @apiParam {String} target 目标.
 *
 * @apiSuccess {int} code 返回状态码（1：修改成功，2：修改失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/editAdvert', async (ctx) => {
    try {
        let data = ctx.request.body;
        let title = data.title,
            type = data.type,
            target = data.target,
            id = data.id;
        if (!title || !type || !target || !id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await advertService.handleEditAdvert(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/advert/deleteAdvert 删除广告
 * @apiName deleteAdvert
 * @apiGroup advert
 *
 * @apiParam {String} id 类目id.
 *
 * @apiSuccess {int} code 返回状态码（1：删除成功，2：删除失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/deleteAdvert', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await advertService.handleDeleteAdvert(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/advert/getAdvert 获取广告列表
 * @apiName getAdvert
 * @apiGroup advert
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：获取失败，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {Object[]} data 获取成功时返回的数据
 * @apiSuccess {String} data.id 广告id
 * @apiSuccess {String} data.title 广告标题
 * @apiSuccess {String} data.type 广告类型
 * @apiSuccess {String} data.target 对象
 * @apiSuccess {String} data.name 对象名
 * @apiSuccess {String} data.url 对象图片
 * @apiSuccess {time} data.time 创建时间
 * @apiSuccess {String} data.state 状态（0：未删除或已启用，1：已删除，2：已停用）
 *
 */

router.post('/getAdvert', async (ctx) => {
    try {
        let result;
        result = await advertService.handleGetAdvert();
        let data = result.data;
        let len = data.length;
        for (let i = 0; i < len; i++) {
            let tempData = {};
            tempData.target = data[i].target;
            tempData.type = data[i].type;//1：案例，2：服务
            let resultData;
            resultData = await advertService.handleGetTargetData(tempData);
            if (resultData.code == 1) {
                result.data[i].name = resultData.data[0].name;
                result.data[i].url = resultData.data[0].url;
            } else {
                result.data[i].name = "null";
                result.data[i].url = "null";
            }
        }
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

module.exports = router;